//
//  LoginKit.m
//  Share
//
//  Created by CPX on 2018/10/15.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "LoginKit.h"
#import "TwitterKit/TWTRKit.h"
#import "LoginResult.h"
#import "FBSDKLoginKit.h"
#import "UIViewController+ShareKitHelper.h"
#import "GoogleSignIn/GoogleSignIn.h"

/**
 登录模块
 @author yzl
 */
@interface LoginKit ()<GIDSignInDelegate,GIDSignInUIDelegate>

@property (nonatomic, strong) LoginSuccess success;
@property (nonatomic, strong) LoginError loginError;

@end

@implementation LoginKit

+ (instancetype)sharedKit{
    static dispatch_once_t onceToken;
    static LoginKit *kit = nil;
    dispatch_once(&onceToken, ^{
        kit = [LoginKit new];
    });
    return kit;
}

+ (void)FBLoginWithSuccess:(LoginSuccess)success error:(LoginError)loginError{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    UIViewController *ctr = [UIViewController currentViewController];
    if (ctr) {
        [login
         logInWithReadPermissions: @[@"public_profile"]
         fromViewController:ctr
         handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                 loginError(error);
             } else if (result.isCancelled) {
                 loginError(nil);
             } else {
                 
                 LoginResult *lresult = [LoginResult new];
                 lresult.userId = result.token.userID;
                 lresult.token = result.token.tokenString;
                 success(lresult);
             }
         }];
    }
}

+ (void)TwitterLoginWithSuccess:(LoginSuccess)success error:(LoginError)loginError{
    [[TWTRTwitter sharedInstance] logInWithCompletion:^(TWTRSession * _Nullable session, NSError * _Nullable error) {
        if (error) {
            loginError(error);
            return ;
        }
        LoginResult *result = [LoginResult new];
        result.userId = session.userID;
        result.userName = session.userName;
        result.token = session.authToken;
        result.tokenSecret = session.authTokenSecret;
        success(result);
    }];
}

+ (void)GoogleLoginWithSuccess:(LoginSuccess)success error:(LoginError)loginError{
    [GIDSignIn sharedInstance].delegate = [LoginKit sharedKit];
    [GIDSignIn sharedInstance].uiDelegate = [LoginKit sharedKit];
    [[GIDSignIn sharedInstance] signIn];
}

#pragma mark - google


- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
    
}

// Present a view that prompts the user to sign in with Google
- (void)signIn:(GIDSignIn *)signIn
presentViewController:(UIViewController *)viewController {
    [[UIViewController currentViewController] presentViewController:viewController animated:YES completion:nil];
}

// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn
dismissViewController:(UIViewController *)viewController {
    [[UIViewController currentViewController] dismissViewControllerAnimated:YES completion:nil];
}

- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
    if (error) {
        self.loginError(error);
    }
    else{
        LoginResult *result = [LoginResult new];
        result.userId = user.userID;
        result.token = user.authentication.idToken;
        result.userName = user.profile.name;
        result.email = user.profile.email;
        self.success(result);
    }
}

- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
    // Perform any operations when the user disconnects from app here.
    // ...
}

@end
